﻿using HeBianGu.Base.WpfBase;
using HeBianGu.General.WpfControlLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shell;

namespace HeBianGu.Application.MainWindow
{
    /// <summary> 说明</summary>
    internal class ShellViewModel : NotifyPropertyChanged
    {
        #region - 属性 -

        private ObservableCollection<TestViewModel> _collection = new ObservableCollection<TestViewModel>();
        /// <summary> 说明  </summary>
        public ObservableCollection<TestViewModel> Collection
        {
            get { return _collection; }
            set
            {
                _collection = value;
                RaisePropertyChanged("Collection");
            }
        }



        private CustomNameViewModel _customNameViewModel = new CustomNameViewModel();
        /// <summary> 说明  </summary>
        public CustomNameViewModel CustomNameViewModel
        {
            get { return _customNameViewModel; }
            set
            {
                _customNameViewModel = value;
                RaisePropertyChanged("CustomNameViewModel");
            }
        }



        private ObservableCollection<Student> _students = new ObservableCollection<Student>();
        /// <summary> 说明  </summary>
        public ObservableCollection<Student> Students
        {
            get { return _students; }
            set
            {
                _students = value;
                RaisePropertyChanged("Students");
            }
        }



        private ObservableCollection<double> _datas = new ObservableCollection<double>();
        /// <summary> 说明  </summary>
        public ObservableCollection<double> Datas
        {
            get { return _datas; }
            set
            {
                _datas = value;
                RaisePropertyChanged("Datas");
            }
        }

        private ObservableCollection<double> _xdatas = new ObservableCollection<double>();
        /// <summary> 说明  </summary>
        public ObservableCollection<double> xDatas
        {
            get { return _xdatas; }
            set
            {
                _xdatas = value;
                RaisePropertyChanged("xDatas");
            }
        }

        private ObservableCollection<double> _xAxis = new ObservableCollection<double>();
        /// <summary> 说明  </summary>
        public ObservableCollection<double> xAxis
        {
            get { return _xAxis; }
            set
            {
                _xAxis = value;
                RaisePropertyChanged("xAxis");
            }
        }

        private ObservableCollection<double> _xGridAxis = new ObservableCollection<double>();
        /// <summary> 说明  </summary>
        public ObservableCollection<double> xGridAxis
        {
            get { return _xGridAxis; }
            set
            {
                _xGridAxis = value;
                RaisePropertyChanged("xGridAxis");
            }
        }

        private ObservableCollection<string> _autoCompleteSource = new ObservableCollection<string>();
        /// <summary> 说明  </summary>
        public ObservableCollection<string> AutoCompleteSource
        {
            get { return _autoCompleteSource; }
            set
            {
                _autoCompleteSource = value;
                RaisePropertyChanged("AutoCompleteSource");
            }
        }


        private ObservableCollection<string> _autoCompleteSelecteds = new ObservableCollection<string>();
        /// <summary> 说明  </summary>
        public ObservableCollection<string> AutoCompleteSelecteds
        {
            get { return _autoCompleteSelecteds; }
            set
            {
                _autoCompleteSelecteds = value;
                RaisePropertyChanged("AutoCompleteSelecteds");
            }
        }


        private ImageSource _imageSource;
        /// <summary> 说明  </summary>
        public ImageSource ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                RaisePropertyChanged("ImageSource");
            }
        }

        private ObservableCollection<SystemInfoModel> _selectedItems = new ObservableCollection<SystemInfoModel>();
        /// <summary> 说明  </summary>
        public ObservableCollection<SystemInfoModel> SelectedItems
        {
            get { return _selectedItems; }
            set
            {
                _selectedItems = value;
                RaisePropertyChanged("SelectedItems");
            }
        }



        private TextBoxViewModel _textBoxViewModel = new TextBoxViewModel();
        /// <summary> 说明  </summary>
        public TextBoxViewModel TextBoxViewModel
        {
            get { return _textBoxViewModel; }
            set
            {
                _textBoxViewModel = value;
                RaisePropertyChanged("TextBoxViewModel");
            }
        }


        private Func<double, string> _xConvert;

        /// <summary> 说明  </summary>
        public Func<double, string> xConvert
        {
            get { return _xConvert; }
            set
            {
                _xConvert = value;
                RaisePropertyChanged("xConvert");
            }
        }




        #endregion

        #region - 命令 -

        #endregion

        #region - 方法 -

        protected override void Init()
        {
            base.Init();

            for (int i = 0; i < 2000; i++)
            {
                this.Students.Add(Student.Random());
            }

            //ICollectionView vw = CollectionViewSource.GetDefaultView(this.Students);

            //vw.GroupDescriptions.Add(new PropertyGroupDescription("Class"));

            _xConvert = l =>
              {  
                  //return $"10^{l}"; 
                  return l.ToString();
              };

            double count = 1000.0 * 1000.0;

            for (int i = 1; i < count; i++)
            {
                //this.Datas.Add(random.NextDouble() * 10);

                var log = Math.Log10(i);
                this.Datas.Add(Convert.ToDouble(i) / (count / 10.0));
                this.xDatas.Add(log);

                int c = (int)(log);

                if (log % 1 == 0)
                {
                    this.xAxis.Add(log);
                }

                if (i % Math.Pow(10, c) == 0)
                {
                    this.xGridAxis.Add(log);

                    //this.xAxis.Add(log);
                }
                
            }

            double p = Math.Log10(count);

            double v = p % 1 == 0 ? p : p + 1;

            this.xAxis.Add((int)v);
            this.xGridAxis.Add((int)v);

            for (int i = 0; i < 10; i++)
            {
                _autoCompleteSource.Add(random.Next(0, 10000000).ToString());
            }

            //_autoCompleteSource.Add("测试");
            //_autoCompleteSource.Add("成功");
            //_autoCompleteSource.Add("失败");
            //_autoCompleteSource.Add("123456789");
            _autoCompleteSource.Add("abcdefghijklmnopqrstuvwxyz");




            _selectedItems.CollectionChanged += (l, k) =>
              {
                  Debug.WriteLine(this.SelectedItems.Count);
              };

        }

        Random random = new Random();

        protected override async void RelayMethod(object obj)
        {
            string command = obj?.ToString();

            //  Do：应用
            if (command == "Add")
            {
                this.Collection.Add(new TestViewModel() { Value = (this.Collection.Count + 1).ToString() });

            }
            //  Do：取消
            else if (command == "Delete")
            {

                this.Collection.RemoveAt(0);

            }

            //  Do：取消
            else if (command == "ToolBar.ValueChanged.RefreshData")
            {



            }
            //  Do：取消
            else if (command == "Chart.MouseWheel.ChangeValue")
            {

            }

            //  Do：取消
            else if (command == "TaskBar")
            {
                //MessageService.ShowTaskbarPercent(l =>
                //{
                //    for (int i = 0; i < 100; i++)
                //    {
                //        l.ProgressState = TaskbarItemProgressState.Normal;

                //        l.ProgressValue = i / 100.0;

                //        Thread.Sleep(10);
                //    }

                //    l.ProgressValue = 0;
                //});

                await MessageService.ShowTaskbarWaitting(() =>
                 {
                     for (int i = 0; i < 100; i++)
                     {
                         Thread.Sleep(50);
                     }

                     return true;
                 });

            }

            //  Do：取消
            else if (command == "ScrollViewer.ScrollingTrigger.ToButton")
            {
                MessageService.ShowWinInfoMessage("移动到了指定按钮18");
            }
            //  Do：取消
            else if (string.IsNullOrEmpty(command))
            {
                MessageService.ShowWinInfoMessage("测试");
            }

            if (obj is MouseWheelEventArgs args)
            {

            }

        }

        #endregion
    }

    /// <summary> 说明</summary>
    internal class CustomNameViewModel : NotifyPropertyChanged
    {
        #region - 属性 -
        private MyEnum _selectedMyEnum;
        /// <summary> 说明  </summary>
        [TypeConverter(typeof(EnumConverter))]
        public MyEnum SelectedMyEnum
        {
            get { return _selectedMyEnum; }
            set
            {
                _selectedMyEnum = value;
                RaisePropertyChanged("SelectedMyEnum");
            }
        }
        #endregion

        #region - 命令 -

        #endregion

        #region - 方法 -

        protected override void RelayMethod(object obj)
        {
            string command = obj.ToString();

            //  Do：应用
            if (command == "Sumit")
            {


            }
            //  Do：取消
            else if (command == "Cancel")
            {


            }
        }

        #endregion
    }

    internal class TextBoxViewModel : ValidationPropertyChanged
    {
        private string _name;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^[\u4e00-\u9fa5]{0,}$", ErrorMessage = "只能输入汉字！")]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;

                RaisePropertyChanged("Name");
            }
        }


        private string _age;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^[0-9]*$", ErrorMessage = "只能输入数字")]
        public string Age
        {
            get { return _age; }
            set
            {
                _age = value;
                RaisePropertyChanged("Age");
            }
        }


        private string _email;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "邮箱地址不合法！")]
        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                RaisePropertyChanged("Email");
            }
        }


        private string _phone;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^1[3|4|5|7|8][0-9]{9}$", ErrorMessage = "手机号码不合法！")]
        public string Phone
        {
            get { return _phone; }
            set
            {
                _phone = value;
                RaisePropertyChanged("Phone");
            }
        }


        private string _account;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^[a-zA-Z][a-zA-Z0-9_]{4,15}$", ErrorMessage = "字母开头，允许5-16字节，允许字母数字下划线！")]
        public string Accont
        {
            get { return _account; }
            set
            {
                _account = value;
                RaisePropertyChanged("Accont");
            }
        }


        private string _passWord;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^[a-zA-Z]\w{5,17}$", ErrorMessage = "以字母开头，长度在6~18之间，只能包含字母、数字和下划线！！")]
        public string PassWord
        {
            get { return _passWord; }
            set
            {
                _passWord = value;
                RaisePropertyChanged("PassWord");
            }
        }


        private string _regin;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^\d{5}$", ErrorMessage = "只能5位的数字")]
        public string Regin
        {
            get { return _regin; }
            set
            {
                _regin = value;
                RaisePropertyChanged("Regin");
            }
        }


        private string _limit;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^\d{5,8}$", ErrorMessage = "只能5-8位的数字")]
        public string Limit
        {
            get { return _limit; }
            set
            {
                _limit = value;
                RaisePropertyChanged("Limit");
            }
        }


        private string _cardID;
        /// <summary> 说明  </summary>
        [Required(ErrorMessage = "数据不能为空")]
        [RegularExpression(@"^\d{15}|\d{18}$", ErrorMessage = "身份证号码不合法！")]
        public string CardID
        {
            get { return _cardID; }
            set
            {
                _cardID = value;
                RaisePropertyChanged("CardID");
            }
        }

        #region - 方法 -

        protected override void RelayMethod(object obj)
        {
            string command = obj.ToString();

            //  Do：应用
            if (command == "Button.Click.CheckDataSumit")
            {
                if (this.IsValid())
                {
                    MessageService.ShowSnackMessageWithNotice("数据校验成功！");
                }
                else
                {
                    MessageService.ShowSnackMessageWithNotice("数据校验错误 - " + this.Error);
                }

            }
            //  Do：取消
            else if (command == "Cancel")
            {


            }
        }

        #endregion

    }

    public class LeagueList : List<League>
    {
        public LeagueList()
        {
            for (int i = 0; i < 3; i++)
            {
                this.Add(new League());
            }
        }
    }

    public class League
    {
        public string Name { get; set; }

        public List<Division> Divisions { get; set; } = new List<Division>();

        public static int Count = 0;

        public League()
        {
            Count++;

            this.Name = this.GetType().Name + Count;

            for (int i = 0; i < 5; i++)
            {
                Divisions.Add(new Division());
            }
        }
    }

    public class Division
    {
        public string Name { get; set; }

        public List<Team> Teams { get; set; } = new List<Team>();

        public static int Count = 0;

        public Division()
        {
            Count++;

            this.Name = this.GetType().Name + Count;

            for (int i = 0; i < 5; i++)
            {
                Teams.Add(new Team());
            }
        }
    }

    public class Team
    {
        public static int Count = 0;

        public string Name { get; set; }

        public Team()
        {
            Count++;

            this.Name = this.GetType().Name + Count;
        }
    }
}
